package com.googlecode.framework.web.pagination;

import org.springframework.context.support.ResourceBundleMessageSource;

import com.googlecode.framework.core.pagination.Paginator;


public class KeyboardableWebPagination extends AbstractWebPagination {

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = -8726570444277146590L;
    
    ResourceBundleMessageSource resourceMessage;
    {
        resourceMessage = new ResourceBundleMessageSource();
        resourceMessage.setBasename(KeyboardableWebPagination.class.getName());
    }

    public KeyboardableWebPagination(Href href, Paginator paginator) {
        super(href, paginator);
    }
    
    /**
     * 
     *   // 开一个滑动窗口。我们经常要在web页面上显示一串的相邻页码，供用户选择。
     *   //        ____________________________________________________________
     *   // 例如:  <<     <       3     4    5    6    [7]    8    9    >    >>
     *   //        ^      ^                             ^               ^    ^
     *   //       第一页 前一页                       当前页          后一页 最后一页
     *   
     * <div class="pagebar">
     *  <ul>
     *      <li> total:9999, size of perpage:20 </li>
     *      <li> <a href="javascript:turnpage(1);"> << </a></li>
     *      <li> <a href="javascript:turnpage(6);"> < </a></li>
     *      <li> <a href="javascript:turnpage(3);"> 3 </a></li>
     *      <li> <a href="javascript:turnpage(4);"> 4 </a></li>
     *      <li> <a href="javascript:turnpage(5);"> 5 </a></li>
     *      <li> <a href="javascript:turnpage(6);"> 6 </a></li>
     *      <li> <a href="javascript:void(0);"> [7] </a></li>
     *      <li> <a href="javascript:turnpage(8);"> 8 </a></li>
     *      <li> <a href="javascript:turnpage(9);"> 9 </a></li>
     *      <li> <a href="javascript:turnpage(10);"> > </a></li>
     *      <li> <a href="javascript:turnpage(999);"> >> </a></li>
     *  </ul>
     * </div>
     */
    @Override
    public String getHtmlPageToolbar() {
        StringBuilder html = new StringBuilder();
        html.append("<div class=\"pagebar\">").append("\n");
        html.append("\t").append("<ul>").append("\n");
        
        // 统计总数
        String totalMessage = resourceMessage.getMessage("__pagination.total", new Object[]{paginator.getItems(), paginator.getPages()}, null);
        html.append("\t\t").append(totalMessage).append("\n");
        // First Page
        if (paginator.isDisabledPage(paginator.getFirstPage())) {
            String inactiveFirstPage = resourceMessage.getMessage("__pagination.first.inactive", null, null);
            html.append("\t\t").append(inactiveFirstPage).append("\n");
        } else {
            String activeFirstPage = resourceMessage.getMessage("__pagination.first", new Object[]{paginator.getFirstPage()}, null);
            html.append("\t\t").append(activeFirstPage).append("\n");
        }
        // Previous Page
        if (paginator.isDisabledPage(paginator.getPreviousPage())) {
            String inactivePreviousPage = resourceMessage.getMessage("__pagination.previous.inactive", null, null);
            html.append("\t\t").append(inactivePreviousPage).append("\n");
        } else {
            String activePreviousPage = resourceMessage.getMessage("__pagination.previous", new Object[]{paginator.getPreviousPage()}, null);
            html.append("\t\t").append(activePreviousPage).append("\n");
        }
        // Page No
        int[] pages = paginator.getSlider();
        for (int page : pages) {
            if (paginator.isDisabledPage(page)) {
                String inactivePage = resourceMessage.getMessage("__pagination.page.inactive", new Object[]{page}, null);
                html.append("\t\t").append(inactivePage).append("\n");
            } else {
                String activePage = resourceMessage.getMessage("__pagination.page", new Object[]{page, page}, null);
                html.append("\t\t").append(activePage).append("\n");
            }
        }
        // Next Page
        if (paginator.isDisabledPage(paginator.getNextPage())) {
            String inactiveNextPage = resourceMessage.getMessage("__pagination.next.inactive", null, null);
            html.append("\t\t").append(inactiveNextPage).append("\n");
        } else {
            String activeNextPage = resourceMessage.getMessage("__pagination.next", new Object[]{paginator.getNextPage()}, null);
            html.append("\t\t").append(activeNextPage).append("\n");
        }
        // Last Page
        if (paginator.isDisabledPage(paginator.getLastPage())) {
            String inactiveLastPage = resourceMessage.getMessage("__pagination.last.inactive", null, null);
            html.append("\t\t").append(inactiveLastPage).append("\n");
        } else {
            String activeLastPage = resourceMessage.getMessage("__pagination.last", new Object[]{paginator.getLastPage()}, null);
            html.append("\t\t").append(activeLastPage).append("\n");
        }
        
        html.append("\t").append("</ul>");
        html.append("</div>");
        return html.toString();
    }
}
